package com.kedi.welfarecenter.controller.operationcenter;

import com.kedi.welfarecenter.enums.ResultEnum;
import com.kedi.welfarecenter.exception.CommonException;
import com.kedi.welfarecenter.service.operationcenter.TemporaryBenefitService;
import com.kedi.welfarecenter.utils.ResultVoUtil;
import com.kedi.welfarecenter.vo.ResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
* @Description:    临时性福利
* @Author:         ytw
* @CreateDate:     2019/7/23 16:07
* @UpdateRemark:   修改内容
* @Version:        1.0
*/
@RestController
public class TemporaryBenefitController {
    private static Logger logger = LoggerFactory.getLogger(TemporaryBenefitController.class);
    @Autowired
    TemporaryBenefitService temporaryBenefitService;
    /**
     * 方法实现说明  查找所有临时性福利列表
     * @author      ytw
     *@param
     * @return
     * @exception
     * @date        2019/7/26 9:34
     */
    @GetMapping("operation/temporaryBenefitList")
    public List<Map<String, Object>> selectcps(){
        List<Map<String, Object>> list = temporaryBenefitService.selectcps();
        if(list.isEmpty()){
            logger.info("查找所有临时性福利列表为空");
            throw new CommonException(ResultEnum.LIST_NOT_EXISTED);
        }else{
            return list;
        }
    }

    /**
     * 方法实现说明  搜索框  根据机构名查找
     * @author      ytw
     *@param         welfaretitle
     * @return
     * @exception
     * @date        2019/7/26 9:37
     */
    @GetMapping("operation/temporaryBenefitListBy")
    public List<Map<String, Object>> selectcpsByName(@RequestParam("welfaretitle") String welfaretitle){
        List<Map<String, Object>> list = temporaryBenefitService.selectcpsByName(welfaretitle);
        if(list.isEmpty()){
            logger.info("查找所有临时性福利为空");
            throw new CommonException(ResultEnum.LIST_NOT_EXISTED);
        }else{
            return list;
        }
    }

    /**
     * 方法实现说明  根据临时性福利主键id 查找详情
     * @author      ytw
     *@param         welfareId
     * @return
     * @exception
     * @date        2019/7/26 9:40
     */
    @GetMapping("operation/temporaryBenefitDetails")
    public List<Map<String, Object>> selectcpsDetails(@RequestParam("welfareId") Integer welfareId){
        List<Map<String, Object>> list = temporaryBenefitService.selectcpsDetails(welfareId);
        if(list.isEmpty()){
            logger.info("查找所有临时性福利列表为空");
            throw new CommonException(ResultEnum.LIST_NOT_EXISTED);
        }else{
            return list;
        }
    }

    /**
     * 方法实现说明  根据领取人员表主键 返回领取人电话
     * @author      ytw
     *@param         welfarereceiverid
     * @return
     * @exception
     * @date        2019/7/26 9:41
     */
    @GetMapping("operation/temporaryBenefitPhone")
    public Map<String, Object> selectcpsPhone(@RequestParam("welfarereceiverid") Integer welfarereceiverid){
        Map<String, Object> map = temporaryBenefitService.selectcpsPhone(welfarereceiverid);
        if(map.isEmpty()){
            logger.info("查找所有老年社会救助列表为空");
            throw new CommonException(ResultEnum.LIST_NOT_EXISTED);
        }else{
            return map;
        }
    }

    /**
     * 方法实现说明   新增临时性福利事项
     * @author      ytw
     *@param         welfaretitle,lowage,heighage,name,price,givetime
     * @return
     * @exception
     * @date        2019/7/26 9:48
     */
    @PostMapping("operation/temporaryBenefitCreatecps")
    public ResultVo createcps(@RequestParam(value = "welfaretitle") String welfaretitle, @RequestParam("lowage") Integer lowage,
                              @RequestParam("heighage") Integer heighage, @RequestParam("name") String name,
                              @RequestParam(value = "price",required = false,defaultValue ="") Integer price, @RequestParam("givetime") String givetime){
        Integer createcps = temporaryBenefitService.createcps(welfaretitle, lowage, heighage, name, price, givetime);
        if(createcps==0){
            logger.error("新增临时性福利事项失败");
            throw new CommonException(ResultEnum.SAVE_DATA_ERROR);
        }
        return  ResultVoUtil.success(createcps);
    }

    /**
     * 方法实现说明  根据福利表主键修改
     * @author      ytw
     *@param        welfaretitle,lowage,heighage,name(现金，或者什么物品),price(现金多少钱，或者物品值多少钱),givetime,welfareid
     * @return
     * @exception
     * @date        2019/7/26 9:59
     */
    @PostMapping("operation/temporaryBenefitUpdatecps")
    public ResultVo updatecps(@RequestParam(value = "welfaretitle") String welfaretitle,@RequestParam("lowage") Integer lowage,
                             @RequestParam("heighage") Integer heighage,@RequestParam("name") String name,
                             @RequestParam(value = "price",required = false,defaultValue ="") Integer price,@RequestParam(value = "givetime",required = false) String givetime,
                             @RequestParam("welfareid") Integer welfareid){
        Integer updatecps = temporaryBenefitService.updatecps(welfaretitle, lowage, heighage, name, price, givetime, welfareid);
        if(updatecps==0){
            logger.info("修改临时性福利事项失败");
            throw new CommonException(ResultEnum.SAVE_DATA_ERROR);
        }
        return  ResultVoUtil.success(updatecps);

    }

}
